﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.HVAC
{
	public partial class FrmSplitPipe : System.Windows.Forms.Form
	{
		public string FsName
		{
			get
			{
				return this.fsName;
			}
			set
			{
				this.fsName = value;
			}
		}

		public FrmSplitPipe(Autodesk.Revit.DB.Document Doc)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.doc = Doc;
			base.Icon = Resource.YArchitechIcon;
			this.ReadSystemPara();
			this.setSectionComboxEnable();
			this.SetCmbCommonTypeAndLength(this.familyType);
		}

		public FrmSplitPipe(Autodesk.Revit.DB.Document Doc, Element ele)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.doc = Doc;
			base.Icon = Resource.YArchitechIcon;
			this.ReadSystemPara();
			this.setSectionComboxEnable();
			this.SetCmbCommonTypeAndLength(this.GetElementType(this.doc, ele));
			this.SetSymbolNameAndCmbProjectType(ele);
			this.SetNumberMode(ele);
		}

		private void radiob4_CheckedChanged(object sender, EventArgs e)
		{
			this.setRadioEnable();
		}

		private void radioButton5_CheckedChanged(object sender, EventArgs e)
		{
			this.setRadioEnable();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			this.WriteSystemPara();
			this.IsPickup = true;
			base.Close();
		}

		private void button2_Click(object sender, EventArgs e)
		{
			if (!this.radioButton1.Enabled && this.radioButton1.Checked)
			{
				YJKMessageBox.Information("不能再3D视图中使用按区域拆分！");
				return;
			}
			if (this.radiob4.Checked && this.cmbProjectType.SelectedItem != null && this.cmbProjectType.SelectedItem.ToString().Trim() == "无")
			{
				YJKMessageBox.Information("活接头不可以设成无！");
				return;
			}
			if (this.radioButton1.Checked)
			{
				this.SectionType = SectionType.Area;
			}
			else if (this.radioButton2.Checked)
			{
				this.SectionType = SectionType.Level;
			}
			else if (this.radioButton3.Checked)
			{
				this.SectionType = SectionType.Project;
			}
			this.IsPickup = false;
			base.DialogResult = DialogResult.OK;
			this.WriteSystemPara();
		}

		private void button3_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
			base.Close();
		}

		private void cbNumber_CheckedChanged(object sender, EventArgs e)
		{
			bool @checked = this.cbNumber.Checked;
			if (!this.canModNumberMode)
			{
				return;
			}
			this.GpNumMode.Enabled = @checked;
		}

		private void setRadioEnable()
		{
			if (this.radiob4.Checked)
			{
				this.cmbProjectType.Enabled = true;
				this.cmbCommonType.Enabled = false;
				return;
			}
			this.cmbProjectType.Enabled = false;
			this.cmbCommonType.Enabled = true;
		}

		private void setSectionComboxEnable()
		{
			if ((int)this.doc.ActiveView.ViewType == 4)
			{
				this.radioButton1.Enabled = false;
				return;
			}
			this.radioButton1.Enabled = true;
		}

		private FamilyType GetElementType(Autodesk.Revit.DB.Document doc, Element ele)
		{
			Element element = doc.GetElement(ele.GetTypeId());
			if (element is PipeType)
			{
				return FamilyType.Pipe;
			}
			if (element is DuctType)
			{
				return FamilyType.Duct;
			}
			if (element is ConduitType)
			{
				return FamilyType.Conduit;
			}
			if (element is CableTrayType)
			{
				return FamilyType.CableTray;
			}
			return FamilyType.None;
		}

		private void SetCmbCommonTypeAndLength(FamilyType type)
		{
			List<string> list = new List<string>();
			switch (type)
			{
			case FamilyType.Pipe:
				list.Add("活接头-热熔");
				list.Add("活接头-丝接");
				list.Add("活接头-粘接");
				list.Add("活接头-承插");
				list.Add("活接头-焊缝");
				list.Add("活接头-钢活套");
				list.Add("活接头-法兰");
				list.Add("活接头-卡箍");
				this.hyTextBox2.Text = "6.0";
				this.hyTextBox3.Text = "1.0";
				break;
			case FamilyType.Duct:
				list.Add("矩形法兰");
				list.Add("矩形活接头");
				list.Add("圆形活接头");
				list.Add("椭圆形活接头");
				this.hyTextBox2.Text = "1.2";
				this.hyTextBox3.Text = "1.0";
				break;
			case FamilyType.Conduit:
				list.Add("线管接头 - PVC");
				list.Add("线管接线盒 - 活接头 - PVC");
				this.hyTextBox2.Text = "3.0";
				this.hyTextBox3.Text = "1.0";
				break;
			case FamilyType.CableTray:
				list.Add("槽式电缆桥架活接头");
				list.Add("梯级式电缆桥架活接头");
				list.Add("托盘式电缆桥架活接头");
				this.hyTextBox2.Text = "2.0";
				this.hyTextBox3.Text = "1.0";
				break;
			}
			this.cmbCommonType.DataSource = list;
		}

		private void SetNumberMode(Element ele)
		{
			if (this.doc.GetElement(ele.GetTypeId()) != null)
			{
				this.txtSymbolName.Text = ((Autodesk.Revit.DB.ElementType)this.doc.GetElement(ele.GetTypeId())).Name;
				this.canModNumberMode = true;
				if (ele is Conduit || ele is CableTray)
				{
					this.GpNumMode.Enabled = false;
					this.rbNumberByPipe.Checked = true;
					this.canModNumberMode = false;
				}
			}
		}

		private void SetSymbolNameAndCmbProjectType(Element ele)
		{
			if (this.doc.GetElement(ele.GetTypeId()) != null)
			{
				this.txtSymbolName.Text = ((Autodesk.Revit.DB.ElementType)this.doc.GetElement(ele.GetTypeId())).Name;
				if (ele is Pipe || ele is Duct)
				{
					this.SetCmbProjectTypeByPipeOrDuct(ele);
					return;
				}
				if (ele is Conduit || ele is CableTray)
				{
					this.SetCmbProjectTypeByConduitOrCableTray(ele);
				}
			}
		}

		private void SetCmbProjectTypeByPipeOrDuct(Element ele)
		{
			Autodesk.Revit.DB.ElementType elementType = (Autodesk.Revit.DB.ElementType)this.doc.GetElement(ele.GetTypeId());
			ElementId elementId = null;
			if (elementType is MEPCurveType && ((MEPCurveType)elementType).RoutingPreferenceManager.GetNumberOfRules((Autodesk.Revit.DB.RoutingPreferenceRuleGroupType)5) > 0)
			{
				elementId = ((MEPCurveType)elementType).RoutingPreferenceManager.GetRule((Autodesk.Revit.DB.RoutingPreferenceRuleGroupType)5, 0).MEPPartId;
			}
			if (elementId != null && elementId.IntegerValue > 0)
			{
				FamilySymbol familySymbol = this.doc.GetElement(elementId) as FamilySymbol;
				this.cmbProjectType.Items.Add(familySymbol.FamilyName);
				this.FsName = familySymbol.Name;
				this.cmbProjectType.SelectedIndex = 0;
			}
		}

		private void SetCmbProjectTypeByConduitOrCableTray(Element ele)
		{
			List<Parameter> list = ((Autodesk.Revit.DB.ElementType)this.doc.GetElement(ele.GetTypeId())).GetParameters("活接头").ToList<Parameter>();
			if (list != null && list.Count > 0)
			{
				Parameter parameter = list.FirstOrDefault<Parameter>();
				this.cmbProjectType.Items.Add(parameter.AsValueString());
				this.cmbProjectType.SelectedIndex = 0;
			}
		}

		private void ReadSystemPara()
		{
			if (this.userSetting.ReadValue("SplitPipe", "Section") == "按区域拆分")
			{
				this.radioButton1.Checked = true;
			}
			else if (this.userSetting.ReadValue("SplitPipe", "Section") == "按楼层拆分")
			{
				this.radioButton2.Checked = true;
			}
			else
			{
				this.radioButton3.Checked = true;
			}
			if (this.userSetting.ReadValue("SplitPipe", "Connect") == "项目中读取")
			{
				this.radiob4.Checked = true;
			}
			else
			{
				this.radioButton5.Checked = true;
			}
			this.cbNumber.Checked = (!(this.userSetting.ReadValue("SplitPipe", "SetNum") == "") && Convert.ToBoolean(this.userSetting.ReadValue("SplitPipe", "SetNum")));
			this.GpNumMode.Enabled = this.cbNumber.Checked;
			if (this.userSetting.ReadValue("SplitPipe", "NumberMode") == this.rbNumberBySys.Text)
			{
				this.rbNumberBySys.Checked = true;
				return;
			}
			this.rbNumberByPipe.Checked = true;
		}

		private void WriteSystemPara()
		{
			if (this.radioButton1.Checked)
			{
				this.userSetting.WriteValue("SplitPipe", "Section", this.radioButton1.Text);
			}
			else if (this.radioButton2.Checked)
			{
				this.userSetting.WriteValue("SplitPipe", "Section", this.radioButton2.Text);
			}
			else if (this.radioButton3.Checked)
			{
				this.userSetting.WriteValue("SplitPipe", "Section", this.radioButton3.Text);
			}
			if (this.radiob4.Checked)
			{
				this.userSetting.WriteValue("SplitPipe", "Connect", this.radiob4.Text);
			}
			else
			{
				this.userSetting.WriteValue("SplitPipe", "Connect", this.radioButton5.Text);
			}
			this.userSetting.WriteValue("SplitPipe", "SetNum", this.cbNumber.Checked.ToString());
			if (this.rbNumberBySys.Checked)
			{
				this.userSetting.WriteValue("SplitPipe", "NumberMode", this.rbNumberBySys.Text);
				return;
			}
			this.userSetting.WriteValue("SplitPipe", "NumberMode", this.rbNumberByPipe.Text);
		}

		private string fsName;

		private FamilyType familyType = FamilyType.None;

		private Document doc;

		public bool IsPickup;

		public SectionType SectionType = SectionType.none;

		private HVACUserSetting userSetting = new HVACUserSetting();

		private bool canModNumberMode = true;
	}
}
